<%@page import="bll.CommonBll"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="util.*"%>
<%@page import="dal.*"%>
<%@ page import="model.*,java.util.*"%>
<%
	GenericDal<ProjectModel, ProjectModelExt> dal = new GenericDal<ProjectModel, ProjectModelExt>(
			new ProjectModel());
	ProjectModelExt queryCount = new ProjectModelExt();
	int count = dal.count(queryCount);

// 	int pageIndex = 1;
// 	if (request.getParameter("pageIndex") != null
// 			&& !request.getParameter("pageIndex").isEmpty()) {
// 		try {
// 			pageIndex = Integer.parseInt(request
// 					.getParameter("pageIndex"));
// 		} catch (Exception e) {
// 		}
// 	}
	int pageIndex = MiscUtil.getIntUrlParam(request, "pageIndex", 1);

// 	int pageSize = 5;
// 	if (request.getParameter("pageSize") != null
// 			&& !request.getParameter("pageSize").isEmpty()) {
// 		try {
// 			pageSize = Integer.parseInt(request
// 					.getParameter("pageSize"));
// 		} catch (Exception e) {
// 		}
// 	}
	int pageSize = MiscUtil.getIntUrlParam(request, "pageSize", 5);

	int noOfPage = count / pageSize;
	if (count % pageSize > 0) {
		noOfPage++;
	}

	ProjectModelExt query = new ProjectModelExt();
	query.setLimit(pageSize);
	query.setOffset((pageIndex - 1) * pageSize);
	
	query.setCountAgenda(true);
	query.setCountChecklist(true);
	query.setCountQuotation(true);
	query.setCountPhase(true);
	
	
	List<ProjectModel> list = CommonBll.loadProjectList(query);
	
	//List<ProjectModel> list = dal.selectList(query);
	

// 	boolean embedded = false;
// 	if (request.getParameter("embedded") != null
// 			&& !request.getParameter("embedded").isEmpty()) {

// 		if (request.getParameter("embedded").equalsIgnoreCase("true")
// 				|| request.getParameter("embedded").equalsIgnoreCase(
// 						"1")) {
// 			embedded = true;
// 		}
// 	}	
	boolean embedded = MiscUtil.getBoolUrlParam(request, "embedded", false);
	
%>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Project List</title>
	<%if(!embedded){ %>
		<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
		<link rel="stylesheet" href="css/themes/ui-lightness/jquery.ui.all.css" />
		<link rel="stylesheet" href="css/jquery-dialog.css" />
		
		<script language="javascript" type="text/javascript" src="js/jquery-1.6.1.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
		<script language="javascript" type="text/javascript" src="js/common.js"></script>
	<%} %> 
	<link rel="stylesheet" href="css/listForm.css" />
	<link rel="stylesheet" href="css/common.css" />
	<script language="javascript" type="text/javascript">
		$(document).ready(pageLoaded);
		function pageLoaded()
		{	
			initDeleteConfirmation();
		} 
	</script>
</head>
<body>
	<%
		if (!embedded) {
	%>
<div class="divContainer">	
	<jsp:include page="menu.jsp"></jsp:include>
	<br />
	<a style="font-weight: bold;" href="projectNew.jsp">New project</a>
	<br />
	<br />
	<% } %>
	<div class="divListHeaderBar">NUMBER OF PROJECTS: <%=count%></div>	
	<table class="tblList">
		<tr class="tblListHeader">
			<th class="thListHeader functionColumnNormal">Project</th>
			<th class="thListHeader" colspan="4"></th>
			<th class="thListHeader Id">Id</th>
			<th class="thListHeader">Name</th>
			<th class="thListHeader">Description</th>
			<th class="thListHeader">Project Manager</th>
			<th class="thListHeader">Status</th>
			<th class="thListHeader">Plan Start Date</th>
			<th class="thListHeader">Plan End Date</th>
			<th class="thListHeader">Client</th>
			<th class="thListHeader">Attendees</th>
			<th class="thListHeader">Venue</th>
		</tr>

		<%
			int size = list.size();
			for (int i = 0; i < size; i++) {
				ProjectModel obj = list.get(i);
		%>

		<tr class="trListItem">
			<td class="tdListInt">
				<span><%=i + 1%></span>
				<br/>
				<span class="spanEdit"><a href="project?mode=edit&id=<%=obj.getId()%>">Edit</a></span>
				<br/>
				<span class="spanEdit"><a class="hrefDelete" href="project?mode=delete&id=<%=obj.getId()%>"  onclick="confirmToDelete();">Delete</a></span>
			</td>
			<td class="tdListHref tdAddAgenda">
				<a href="agendaNew.jsp?idProject=<%=obj.getId()%>">Add Agenda</a>
				<br />
				[<%=obj.getAgendaCount()%>]
			</td>
			<td class="tdListHref tdAddChecklist">
				<a href="checklistNew.jsp?idProject=<%=obj.getId()%>">Add Checklist</a>
				<br />
				[<%=obj.getChecklistCount()%>]
			</td>
			<td class="tdListHref tdAddQuotation">
				<a href="quotationNew.jsp?idProject=<%=obj.getId()%>">Add Quotation</a>
				<br />
				[<%=obj.getQuotationCount()%>]
			</td>
			<td class="tdListHref tdAddPhase">
				<a href="phaseNew.jsp?idProject=<%=obj.getId()%>">Add Phase</a>
				<br />
				[<%=obj.getPhaseCount()%>]
			</td>
			<td class="tdList"><%=obj.getIdToString()%></td>
			<td class="tdListText projectName">
				<a href="project.jsp?id=<%=obj.getId()%>"><%=obj.getNameToString()%></a>			
			</td>
			<td class="tdListTextarea"><%=obj.getDescriptionToString()%></td>
			<td class="tdListInt"><%=CacheUtil.getEmployeeName(obj.getPm())%></td>
			<td class="tdListInt"><%=StringUtil.getQuotationStatus(obj.getStatus())%></td>
			<td class="tdListDateTime"><%=StringUtil.toDateString(obj.getPlanStartDate())%></td>
			<td class="tdListDateTime"><%=StringUtil.toDateString(obj.getPlanEndDate())%></td>
			<td class="tdListInt"><%=CacheUtil.getCompanyName(obj.getIdCompany())%></td>
			<td class="tdListInt"><%=obj.getAttendeesToString()%></td>
			<td class="tdListInt"><%=CacheUtil.getVenueName(obj.getIdVenue())%></td>
		<%
			}
		%>
	</table>
	<br />
	<%
		if(noOfPage>1)
		{
	%>
	<table class="tblPaging">
		<tr class="trPaging">
			<td class="tdPaging"> <span>Page: </span>
				<%
					for (int i = 1; i <= noOfPage; i++) {
						if (i == pageIndex) { %> 
							<span class="lblPaging"><%=i%></span> 
						<% } else { %> 
							<a class="hrefPaging" href="projectList.jsp?pageIndex=<%=i%>&pageSize=<%=pageSize%>"><%=i%></a>
					<%
							}
						}
					%>
			</td>
		</tr>
	</table>
	<br />
	<% } %>
	<%if(!embedded){ %> </div> <%} %>
</body>
</html>